/**
 * 服务方法appMethod.js
 * 版本号： v1.0.0
 */
$(function () {
    $("#appMethod_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/appMethod/appMethodAllList.do',
        idField: 'id',
        striped:true,
        frozenColumns: [[
            {field: 'id', checkbox: true}
        ]],
        columns: [
            [
                {field: 'appKey', title: '应用KEY', width: 85, align: 'center', fixed: true},
                {field: 'method', title: '服务方法', width: 450},
                {field: 'rate', title: '调用频率', width: 60, align: 'center', fixed: true},
                {field: 'times', title: '总次数', width: 60, align: 'center', fixed: true},
                {
                    field: 'status',
                    title: '是否启用',
                    width: 60,
                    align: 'center',
                    fixed: true,
                    formatter: function (val) {
                        if (val == 1) return "<span style='color: green; font-weight: bold'>启用</span>";
                        if (val == 0) return "<span style='color: red; font-weight: bold'>禁用</span>";
                    }
                },
                {field: 'createDate', title: '创建时间', width: 135, fixed: true, align: 'center'}
            ]
        ],
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                $("#appMethod_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#appMethod_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            appMethod_add();
                        }
                    });
                }
                //修改按钮
                if ("editBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            appMethod_edit();
                        }
                    });
                }
                //删除按钮
                if ("delBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            appMethod_del();
                        }
                    });
                }
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            appMethod_findList();
                        }
                    });
                }
                //详情按钮
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            appMethod_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
                //重置按钮
                if ("resetBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            appMethod_restAppMethod();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加服务方法方法
 */
function appMethod_add() {
    $("#appMethod_dialog").dialog({
        title: '添加方法',
        width: 460,
        modal: true,
        closed: true,
        height: 310,
        iconCls: "icon-custom-add",
        buttons: [{
            text: "添加",
            iconCls: "icon-role-state-ok",
            handler: function () {
                doOptionAppMethod();
            }
        }, {
            text: "取消",
            iconCls: "icon-role-state-no",
            handler: function () {
                $('#appMethod_dialog').dialog('close');
            }
        }]
    });
    $("#id").val("");
    $("#appKey").textbox('setValue', "");
    $("#method").textbox('setValue', "");
    $("#rate").textbox('setValue', "");
    $("#times").textbox('setValue', "");
    $("#appMethod_dialog").dialog("move", {left: 450, top: 100});
    $("#appMethod_dialog").dialog('open');

}

/**
 * 删除服务方法方法
 */
function appMethod_del() {
    var row = $("#appMethod_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({title: '操作提示', msg: "请选择删除服务方法", timeout: 3000, showType: "slide"});
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.method + ' 服务方法吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/appMethod/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#appMethod_dataList").datagrid("reload", {
                        appKey: $("#appKey_").val(),
                        method: $("#method_").val()
                    });
                }
            })
        }
    });
}

/**
 * 修改服务方法方法
 * @returns {boolean}
 */
function appMethod_edit() {
    var row = $("#appMethod_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({title: '操作提示', msg: "请选择修改服务方法", timeout: 3000, showType: "slide"});
        return;
    }
    $("#appMethod_dialog").dialog({
        title: '修改方法',
        width: 460,
        modal: true,
        closed: true,
        height: 310,
        iconCls: "icon-custom-edit",
        buttons: [{
            text: "修改",
            iconCls: "icon-role-state-ok",
            handler: function () {
                doOptionAppMethod();
            }
        }, {
            text: "取消",
            iconCls: "icon-role-state-no",
            handler: function () {
                $('#appMethod_dialog').dialog('close');
            }
        }]
    });

    $('#appMethod_dialog').dialog('open');
    $("#appMethod_dialog").dialog("move", {left: 450, top: 100});
    $("#appMethodForm").form("load", row);
}

/**
 * 刷新方法
 */
function appMethod_view(iconCls) {
    var row = $("#appMethod_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的方法详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(ffxq.id , ffxq.url + row.id, iconCls, ffxq.closable, ffxq.title);
}


/**
 * 执行保存操作方法
 */
function doOptionAppMethod() {
    var params = $("#appMethodForm").serialize();
    $("#appMethodForm").form("submit", {
        cache: false,
        data: params,
        url: CONTEXT_PATH + "/appMethod/save.do",
        onSubmit: function () {
            return $(this).form("validate");
        },
        success: function (data) {
            var obj = JSON.parse(data);
            messager(obj);
            $('#appMethod_dialog').dialog('close');
            $("#appMethod_dataList").datagrid("reload", {});
        }
    });
}

//查询服务方法方法
function appMethod_findList() {
    $("#appMethod_dataList").datagrid("reload", {
        appKey: $("#appKey_").val(),
        method: $("#method_").val()
    });
    $("#appMethod_dataList").datagrid("clearSelections");
}

/**
 * 重置服务方法
 */
function appMethod_restAppMethod() {
    $.ajax({
        url: CONTEXT_PATH + "/appMethod/resetAccessService.do",
        type: "get",
        success: function (data) {
            //var obj = JSON.parse(data);
            messager(data);
        }
    })

}
